package com.habdqn.jspdemo.im.servlet.company;

import com.habdqn.jspdemo.im.enetity.Company;
import com.habdqn.jspdemo.im.service.CompanyService;
import com.habdqn.jspdemo.im.service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 执行修改公司信息的操作
 * @author Xing Jiejian
 */
@WebServlet(name = "UpdateCompanyServlet",value = "/company/update")
public class UpdateCompanyServlet extends HttpServlet {
    /**
     * 处理更新操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String strId = request.getParameter("id");
        String name = request.getParameter("name");
        String city = request.getParameter("city");
        String source = request.getParameter("source");
        String information = request.getParameter("information");
        String logo = request.getParameter("logo");
        String strSourceId = request.getParameter("sourceId");
        int sourceId = 4;
        if(strSourceId!=null&&!"".equals(strSourceId)){
            sourceId = Integer.parseInt(strSourceId);
        }

        Company company = new Company(Integer.parseInt(strId),name,city,information,source, sourceId, null, logo);

        CompanyService companyService = new CompanyServiceImpl();
        PrintWriter out = response.getWriter();
        if(companyService.update(company)){
            out.print("<script>");
            out.print("alert('更新成功！');");
            out.print("location.href='/company/list';");
            out.print("</script>");
        }else{
            out.print("<script>");
            out.print("alert('更新失败!');");
            out.print("history.back();");
            out.print("</script>");
        }
    }

    /**
     * 获取要更新的公司信息，并跳转到修改页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String strId = request.getParameter("id");
        if(strId!=null&&!"".equals(strId)){
            int id = Integer.parseInt(strId);
            //调用业务方法
            CompanyService companyService = new CompanyServiceImpl();
            Company company = companyService.getById(id);
            if(company!=null) {
                //将数据保存到作用域中转发到更新页面
                request.setAttribute("company", company);
                request.getRequestDispatcher("/company/update.jsp").forward(request, response);
                return;
            }
        }
        //如果传递编号不正确或者没有对应编号的公司
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("<script>");
        out.print("alert('非法的公司编号！')");
        out.print("location.href='/company/list.jsp'");
        out.print("<script>");
    }
}
